package common.library.utils;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.http.SslError;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import common.library.activities.SelectFileActivity;
import common.library.activities.SelectFolderActivity;
import common.library.models.Size;
import common.library.models.StringCallback;
import lombok.var;

public class ApplicationUtil {

    public static Size getScreenSize(Activity activity) {
        var display = activity.getResources().getDisplayMetrics();
        return new Size(display.widthPixels, display.heightPixels);
    }

    public static void openIntent(Activity activity, Class<?> cls) {
        var intent = new Intent(activity, cls);
        activity.startActivity(intent);
    }

    public static void selectFile(Activity activity, StringCallback callback) {
        SelectFileActivity.callback = callback;
        var intent = new Intent(activity, SelectFileActivity.class);
        activity.startActivity(intent);
    }

    public static void selectFolder(Activity activity, StringCallback callback) {
        SelectFolderActivity.callback = callback;
        var intent = new Intent(activity, SelectFolderActivity.class);
        activity.startActivity(intent);
    }

    /**
     * 推荐使用  <activity android:name=".MainActivity" android:screenOrientation="landscape">
     */
    public static void setScreenOrientationLandscape(AppCompatActivity activity) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    /**
     * 使用前,Activity先实现View.OnClickListener
     */
    public static void addClickListener(View.OnClickListener viewOnClickListenedActivity, View view) {
        view.setOnClickListener(viewOnClickListenedActivity);
    }

    /**
     * 使用前,Activity先实现View.OnClickListener
     */
    public static void addClickListener(AppCompatActivity viewOnClickListenedActivity, int id) {
        addClickListener((View.OnClickListener) viewOnClickListenedActivity, viewOnClickListenedActivity.findViewById(id));
    }

    public static void hide(AppCompatActivity activity, final View view) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                view.setVisibility(View.GONE);
            }
        });
    }

    public static void show(View view) {
        view.setVisibility(View.VISIBLE);
    }

    public static void hide(final AppCompatActivity activity, final int id) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                var view = activity.findViewById(id);
                view.setVisibility(View.GONE);
            }
        });
    }

    public static void show(AppCompatActivity activity, int id) {
        var view = activity.findViewById(id);
        view.setVisibility(View.VISIBLE);
    }

    /**
     * 高版本安卓android:usesCleartextTraffic="true"很重要
     */
    public static void loadWebView(WebView webView, String url, final Runnable finishedCallback) {
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        WebViewClient webViewclient = new WebViewClient() {
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            public void onPageFinished(WebView view, String url) {
                if (finishedCallback != null)
                    finishedCallback.run();
            }
        };

        webView.setWebViewClient(webViewclient);
        webView.loadUrl(url);
    }

    public static void loadWebView(final AppCompatActivity activity, final int id, final String url, final Runnable finishedCallback) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                WebView webView = activity.findViewById(id);
                loadWebView(webView, url, finishedCallback);
            }
        });
    }

    public static void loadWebView(final AppCompatActivity activity, final int id, final String url) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                loadWebView(activity, id, url, null);
            }
        });
    }

    public static void exit() {
        System.exit(0);
    }

    public static void quit() {
        exit();
    }

    public static void close() {
        exit();
    }

    public static void showMessage(final AppCompatActivity activity, final String text) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(activity, text, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
